﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.ParentForm;
using System.Collections;
using UI.Lib.Factory;
using UI.Lib;
using Components;

namespace UI.ShopManagement
{
    public partial class FrmBas1a01 : BaseTMain
    {
        FrmBas1a01Edit edit = null;
        bool _isClickNode = false;
        public FrmBas1a01(string modulecode, string action)
            : base(modulecode, action)
        {
            InitializeComponent();
        }

        protected override void OnAddClick(ToolStripButton btn)
        {
            edit = new FrmBas1a01Edit("A", GetTitleStyle(this.Text, btn.Text), "", null);
            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        protected override void OnEditClick(ToolStripButton btn, DataGridViewRow row)
        {
            OpenEdit("M", btn.Text, Components.Validation.ToString(row.Cells["listid"].Value), null);
        }

        protected override void OnViewClick(ToolStripButton btn, DataGridViewRow row)
        {
            OpenEdit("V", btn.Text, Components.Validation.ToString(row.Cells["listid"].Value), null);
        }

        private void OpenEdit(string action, string btnText, string code, string[] keys)
        {
            edit = new FrmBas1a01Edit(action, GetTitleStyle(this.Text, btnText), code, keys);
            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        protected override void UpdatePager(UI.Common.Pager pager)
        {
            pager.FilterAppName = "a.muser";
        }

        /// <summary>
        /// 加载树
        /// </summary>
        protected override void LoadTree()
        {
            Hashtable hs = new Hashtable();
            hs["muser"] = Context.Comocde + '%';

            DataTable areas = CommonHelper.Entity.ExecFun("getarealist", hs);
            if (areas == null || 0 == areas.Rows.Count) return;

            this.treeMain.Nodes.Clear(); //先清空节点

            DataView dtView = new DataView(areas);
            dtView.RowFilter = string.Format("pcode = ''");
            //绑定更节点
            TreeNode nodeFather = null;
            string areacode;
            for (int i = 0; i < dtView.Count; i++)
            {
                areacode = dtView[i]["areacode"].ToString();
                nodeFather = new TreeNode(areacode + "_" + dtView[i]["areaname"].ToString());
                nodeFather.Name = dtView[i]["listid"].ToString();

                //该节点有子节点
                if (Validation.ToString(dtView[i]["haschild"]) == "1")
                    LoadChildNode(areas, nodeFather, Validation.ToString(dtView[i]["listid"]));

                this.treeMain.Nodes.Add(nodeFather);
            }

            this.treeMain.ExpandAll(); //展开所有
        }

        //加载子节点
        private void LoadChildNode(DataTable dtps, TreeNode nodeFather, string plistid)
        {
            TreeNode node = null;

            DataView dtView = new DataView(dtps);
            dtView.RowFilter = string.Format("pcode = '{0}'", plistid);
            string areacode;
            for (int i = 0; i < dtView.Count; i++)
            {
                areacode = dtView[i]["areacode"].ToString();
                node = new TreeNode(areacode + "_" + dtView[i]["areaname"].ToString());
                node.Name = dtView[i]["listid"].ToString();

                //该节点有子节点
                if (Validation.ToString(dtView[i]["haschild"]) == "1")
                    LoadChildNode(dtps, node, Validation.ToString(dtView[i]["listid"]));

                nodeFather.Nodes.Add(node);
            }
        }

        private void cntLeft_Opened(object sender, EventArgs e)
        {
            新增下属区域ToolStripMenuItem.Enabled = false;

            if (_isClickNode) //点击空白区域的话，只允许增加顶级部门
            {
                新增下属区域ToolStripMenuItem.Enabled = true;
            }

            _isClickNode = false;
        }

        private void 新增下属区域ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode node = this.treeMain.SelectedNode;
            string[] text = node.Text.Split('_');
            string code = text.Length > 0 ? text[0] : "";
            string dis = text.Length > 1 ? text[1] : "";
            string[] keys = new string[] { node.Name, code, dis };

            edit = new FrmBas1a01Edit("A", GetTitleStyle(this.Text, "新增"), "", keys);
            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        private void treeMain_MouseClick(object sender, MouseEventArgs e)
        {
            TreeNode node = this.treeMain.GetNodeAt(e.X, e.Y);

            if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right) //显示详情
            {
                _isClickNode = true;
            }

            this.treeMain.SelectedNode = node;
        }
    }
}
